extends VBoxContainer


onready var map_op = $HBoxContainer/mapSearchSelect
onready var entity_label = $HBoxContainer2/entity_label
onready var entity_open_btn = $HBoxContainer2/open
onready var replace_info_input = $TextEdit
onready var trigger_id_input = $HBoxContainer2/triggerSpinBox
onready var visible_op = $HBoxContainer2/OptionButton


var entity_data = null

var entity_text_arr = ["触发器", "NPC", "传送门"]

func _ready():
	var map_list = DataSource.get_data_list(DataSource.map_register)
	
	map_op.set_data("选择地图", map_list)
	



func _on_select_button_up():
	var map_register = DataSource.get_data_by_one(DataSource.map_register, map_op.get_value())
	if map_register:
		var ui = MainTool.getUILayer().add_ui("res://tool/page/map/mapView/MapView.tscn", 0, false)
		ui.set_data(map_register)
		ui.select_entity_mode = true
		ui.connect("select_entity", self, "_on_select_entity")
	else:
		MainTool.toast_info("请先选择地图")


func set_entity_data(data):
	self.entity_data = data
	
	if data:
		var entity_text = entity_text_arr[data["type"]]
		
		var name = ""
		
		if data["type"] == 1:
			var npc_info = DataSource.get_data_by_one(DataSource.map_npc, data["type_id"])
			name = "%s【%d】" % [npc_info.get("name", ""), npc_info["id"]]
		
		if data["type"] == 2:
			var door_info = DataSource.get_data_by_one(DataSource.map_door, data["type_id"])
			name = "%s【%d】" % [door_info.get("name", ""), door_info["id"]]
		
		
		if data["type"] != 1 && data["type"] != 2:
			trigger_id_input.value = data.get("id", -1)
			trigger_id_input.visible = true
		else:
			trigger_id_input.visible = false
		
		
		if data.get("visiable", true):
			visible_op.select(0)
		else:
			visible_op.select(1)
			
		var map_info = DataSource.get_data_by_one(DataSource.map_register, data["map_id"])
		
		var text = "当前实体: %s->%s【%s】(%d, %d)" %[map_info["name"], name, entity_text, data["x"], data["y"]]
		entity_label.text = text
		
		
		entity_open_btn.visible = true
	else:
		entity_label.text = "无实体"
		
		entity_open_btn.visible = false
		

func _on_select_entity(data):
	
	var type = data["type"]
	
	set_entity_data({
			"type": type,
			"map_id": data["map_register"]["id"],
			"type_id": data["data"].get("id", -1),
			"visiable": true,
			"x": data["data"]["x"],
			"y": data["data"]["y"],
		})
		
	
		

func _on_open_button_up():
	var map_register = DataSource.get_data_by_one(DataSource.map_register, entity_data["map_id"])
	
	if map_register:
		var ui = MainTool.getUILayer().add_ui("res://tool/page/map/mapView/MapView.tscn", 0, false)
		ui.set_data(map_register)
		
		ui.select_grid(entity_data["x"], entity_data["y"])


func set_data(data):
	set_entity_data(data)
	
	var replace_info = data.get("replace_info", {})
	
	replace_info_input.text = JSON.print(replace_info, "\t")



func get_data():
	if entity_data:
		entity_data["replace_info"] = str2var(replace_info_input.text)
		
		if trigger_id_input.visible:
			# 触发器ID显示
			var id = trigger_id_input.value
			entity_data["id"] = id
		
		if visible_op.get_selected_id() == 1:
			entity_data["visiable"] = true
		else:
			entity_data["visiable"] = false
		
		return entity_data
	else:
		return null
	

func _on_del_button_up():
	queue_free()
